CREATE TABLE [dbo].[tb_Cuentas]
(
[idCuenta] [int] NOT NULL IDENTITY(1, 1),
[Descripcion] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[TipoCuenta] [int] NULL,
[Organizacion] [int] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_Cuentas] ON [dbo].[tb_Cuentas]
 FOR UPDATE AS 
 IF (update(idCuenta)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_RegistroContable
		 SET Cuenta = (SELECT idCuenta FROM inserted)
		 WHERE tb_RegistroContable.Cuenta = (SELECT idCuenta FROM deleted)
		 UPDATE tb_Balances
		 SET Cuenta = (SELECT idCuenta FROM inserted)
		 WHERE tb_Balances.Cuenta = (SELECT idCuenta FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end
GO
ALTER TABLE [dbo].[tb_Cuentas] ADD CONSTRAINT [PK_tb_Cuentas] PRIMARY KEY NONCLUSTERED  ([idCuenta]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_Cuentas] WITH NOCHECK ADD CONSTRAINT [FK_tb_Cuentas_tb_Organizaciones] FOREIGN KEY ([Organizacion]) REFERENCES [dbo].[tb_Organizaciones] ([idOrganizaciones])
GO
ALTER TABLE [dbo].[tb_Cuentas] WITH NOCHECK ADD CONSTRAINT [FK_tb_Cuentas_tb_TiposDeCuentas] FOREIGN KEY ([TipoCuenta]) REFERENCES [dbo].[tb_TiposDeCuentas] ([idTipoCuenta])
GO
